﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using GoShopping.BLL;
using GoShopping.Model.Enumerations;
using GoShopping.Model;
using System.Data;

namespace GoShopping.Web.SuperManager
{
    public partial class ManagerPageC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] == null)
                {
                    panelForAdd.Visible = true;
                    panelForUpdate.Visible = false;

                    switch (Request.QueryString["Type"].ToString())
                    {
                        case "AddSysM":
                            CreateUserWizard1.CreateUserButtonText = "创建系统管理员";
                            break;
                        case "AddProductM":
                            CreateUserWizard1.CreateUserButtonText = "创建商品管理员";
                            break;
                        case "AddDownM":
                            CreateUserWizard1.CreateUserButtonText = "创建下载管理员";
                            break;
                    }
                }
                else
                {
                    panelForAdd.Visible = false;
                    panelForUpdate.Visible = true;
                    bindUser(Request.QueryString["ID"].ToString());
                }
            }
        }

        /// <summary>
        /// 根据商品管理员ID获取商品管理员信息
        /// </summary>
        /// <param name="userID">ID</param>
        void bindUser(string userID)
        {
            switch (Request.QueryString["Role"].ToString())
            {
                case "ProRole":
                    ProductManagerBean userBean = new ProductManagerBean();
                    GoShopping.Model.ProductManager pr = userBean.GetOneProductManager(Convert.ToInt32(userID));

                    txtUserName.Text = pr.LoginName;
                    txtPhome.Text = pr.Phone;
                    if (pr.Sex == null)
                    {
                        ddlSex.SelectedValue = "01";
                    }
                    break;
                case "UserRole":
                    UserManagerBean userMBean = new UserManagerBean();
                    UserManager um = userMBean.GetOneUserManager(Convert.ToInt32(Request.QueryString["ID"]));
                 
                    txtUserName.Text = um.Name;
                    txtPhome.Text = um.Phone;
                    if (um.Sex == null)
                    {
                        ddlSex.SelectedValue = "01";
                    }
                                       
                    break;                   
            }
            
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("SupMenOperatorL.aspx?index="+Request.QueryString["index"]);
            //switch (((Button)sender).Text)
            //{
            //    case "创建系统管理员":
            //        {
            //            Response.Redirect("SupMenOperatorL.aspx?index=0");

            //        } break;
            //    case "创建商品管理员":
            //        {
            //            Response.Redirect("SupMenOperatorL.aspx?index=1");
            //        } break;
            //    case "创建下载管理员":
            //        {
            //            Response.Redirect("SupMenOperatorL.aspx?index=2");
            //        } break;
            //}
        }

        protected void CreateUserWizard1_CreatingUser(object sender, LoginCancelEventArgs e)
        {
            MemberShipService memberShaipService = new MemberShipService();
            string ProUserName = CreateUserWizard1.UserName.ToString();
            string ProPwd = CreateUserWizard1.Password.ToString();
            string ProEmail = CreateUserWizard1.Email.ToString();
            string passwordQustion = "no";
            string passwordanser = "no";
            MembershipCreateStatus statue = MembershipCreateStatus.Success;

            bool createUser=false;
            int flag = 0;
            switch (Request.QueryString["Type"].ToString())
            {
                case "AddSysM":
                    {
                        createUser = memberShaipService.CreateMember(ProUserName, ProEmail, ProPwd, passwordQustion, passwordanser, true, Member_Roles.Role_UserManager, out statue);
                        flag = 0;
                    } break;
                case "AddProductM":
                    {
                        createUser = memberShaipService.CreateMember(ProUserName, ProEmail, ProPwd, passwordQustion, passwordanser, true, Member_Roles.Role_ProductManager, out statue);
                        flag = 1;
                    } break;
                case "AddDownM":
                    {
                        createUser = memberShaipService.CreateMember(ProUserName, ProEmail, ProPwd, passwordQustion, passwordanser, true, Member_Roles.Role_DownManager, out statue);
                        flag = 2;
                    } break;
            }
            
            if (createUser)
            {
                //会员注册成功，页面跳转到index页面
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "<script>alert('用户信息添加成功！');</script>", false);
                Response.Redirect("SupMenOperatorL.aspx?index=" + flag);
            }
            else
            {
                //注册不成功，输出错误信息，清空注册密码。
                //lbinfo.Visible = true;
                switch (statue.ToString())
                {
                    case "DuplicateEmail":
                        lbinfo.Text = "电子邮件地址已存在于应用程序的数据库中";
                        break;
                    case "DuplicateProviderUserKey":
                        lbinfo.Text = "提供程序用户键值已存在于应用程序的数据库中";
                        break;
                    case "DuplicateUserName":
                        lbinfo.Text = "用户名已存在于应用程序的数据库中";
                        break;
                    case "InvalidAnswer":
                        lbinfo.Text = "密码提示问题答案的格式设置不正确";
                        break;
                    case "InvalidEmail":
                        lbinfo.Text = "电子邮件地址的格式设置不正确";
                        break;
                    case "InvalidPassword":
                        lbinfo.Text = "密码的格式设置不正确";
                        break;
                    case "InvalidProviderUserKey":
                        lbinfo.Text = "提供程序用户键值的类型或格式无效";
                        break;
                    case "InvalidQuestion":
                        lbinfo.Text = "密码提示问题的格式设置不正确";
                        break;
                    case "InvalidUserName":
                        lbinfo.Text = "在数据库中未找到用户名";
                        break;
                    case "ProviderError":
                        lbinfo.Text = "提供程序返回一个未由其他 MembershipCreateStatus 枚举值描述的错误";
                        break;
                    case "Success":
                        lbinfo.Text = "创建用户成功";
                        break;
                    case "UserRejected":
                        lbinfo.Text = "用户不成功,请稍后再试!";
                        break;
                }

            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            switch (Request.QueryString["Role"].ToString())
            {
                case "ProRole":
                    GoShopping.Model.ProductManager pr = new GoShopping.Model.ProductManager();
                    ProductManagerBean ProUserBean = new ProductManagerBean();
                    pr.LoginName = txtUserName.Text.ToString();
                    pr.Phone = txtPhome.Text.ToString();
                    pr.Sex = ddlSex.SelectedItem.Text.ToString();
                    pr.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                    bool staust = ProUserBean.UpdateProductManager(pr);
                    if (staust)
                    {
                        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "<script>alert('用户信息修改成功！');</script>", false);
                        btnUpdate.Visible = false;
                    }
                    else
                    {
                        lbinfo.Text = "修改失败！";
                        lbinfo.Visible = true;
                    }
                    break;
                case "UserRole":
                    UserManager um = new UserManager();
                    um.Name = txtUserName.Text.ToString();
                    um.Phone = txtPhome.Text.ToString();
                    um.Sex = ddlSex.SelectedItem.Text.ToString();
                    um.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                    UserManagerBean umBean = new UserManagerBean();
                    bool isUpdateUM = umBean.UpdateUserManager(um);
                    if (isUpdateUM)
                    {
                        ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "click", "<script>alert('用户信息修改成功！');</script>", false);
                        btnUpdate.Visible = false;
                    }
                    else
                    {
                        lbinfo.Text = "修改失败！";
                        lbinfo.Visible = true;
                    }
                    break;
            }
        }
    }
}
